# -*- coding: utf-8 -*-

'''
	face recognization
'''

import cv2
import os

def get_gray_value(file_path):
	img = cv2.imread(file_path, 0)			# read as gray image
	m, n = img.shape						# get the size of the face-img
	gray_vector = list()					# gray vector
	for i in range(m):						# row
		for j in range(n):					# column
			gray_vector.append(img[i][j])	
	return gray_vector

def write_file(img_id, gray_vector, w_file):
	data_str = img_id + " "
	for i in range(len(gray_vector)):
		data_str = data_str + str(i + 1) + ':' + str(gray_vector[i]) + ' '
	w_file.write(data_str + '\n')


# main function
if __name__ == '__main__':
	file_w_train = open('svm_data_train', 'w')
	file_w_test = open('svm_data_test', 'w')
	face_dir = os.listdir('./orl')
	print(face_dir)
	for item in face_dir:
		img_id = item.split('s')[1]
		print(img_id)
		face_img_path = os.listdir('./orl/' + item)
		print(face_img_path)
		img_index = 1
		for img_path in face_img_path:
			if img_index < 9:
				print('train:./orl/' + item + '/' + img_path)
				write_file(img_id, get_gray_value('./orl/' + item + '/' + img_path), file_w_train)
			else:
				print('test:./orl/' + item + '/' + img_path)
				write_file(img_id, get_gray_value('./orl/' + item + '/' + img_path), file_w_test)
			img_index += 1
	file_w_test.close()
	file_w_train.close()